
import Controller.*;
import TO.*;
import java.util.*;
import java.util.regex.*;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author admin-pc
 */
public class FindMenu {

    public boolean test(String testString) {
        Pattern p = Pattern.compile("(1|2|3|0)");
        Matcher m = p.matcher(testString);
        return m.matches();
    }

    public void find() {
        AuthorTO authModel = new AuthorTO();
        BookTO bookModel = new BookTO();

        Application app = new Application();

        AuthorController ac = new AuthorController(authModel);
        BookController bc = new BookController(bookModel);
        MainMenu mainMenu = new MainMenu();

        SelectAuthorMenuCommand selectAuthorMenuCommand = new SelectAuthorMenuCommand(ac);
        SelectBookMenuCommand selectBookMenuCommand = new SelectBookMenuCommand(bc);
        SelectAllMenuCommand allMenuCommand = new SelectAllMenuCommand(ac, bc);
        MainMenuCommand mainMenuCommand = new MainMenuCommand(mainMenu);

        app.setCommand(1, selectAuthorMenuCommand);
        app.setCommand(2, selectBookMenuCommand);
        app.setCommand(3, allMenuCommand);
        app.setCommand(0, mainMenuCommand);

        System.out.println("\n1 Показать всех авторов");
        System.out.println("2 Показать все книги");
        System.out.println("3 Показать всех авторов и все книги");
        System.out.println("0 Главное меню");

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        try {
            int i = Integer.parseInt(str);

            if (test(str)) {
                app.buttonWasPressed(i);
            }
            else {
                System.out.println("\nВведите корректные данные");
                find();
            }
        }
        catch (Exception e) {
            System.out.println("\nВведите корректные данные");
            find();
        }
        find();
    }

}
